<?php
//拿到access_token
function getToken(){
    $id = C("APPID");
    $secret = C("APPSECRET");
    $date=date("Y-m-d H");
    $arr=M()->query("select * from gzh_access_token where appid='{$id}' and createtime like '{$date}%'");

    if($arr[0]['access_token']){
        return $arr[0]['access_token'];
    }else {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$id}&secret={$secret}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//设置请求地址
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不需要证书验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不需要主机验证
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch);
        $arr = json_decode($json, true);
        $date=date("Y-m-d H");
        M()->query("update gzh_access_token set access_token='{$arr['access_token']}',createtime='{$date}' where appid='{$id}'");
        return $arr['access_token'];

        curl_close($ch);
    }
}

function yibiArt($name,$id2){
        $dataArr=array(
            'id'=>$name,
            'idi'=>'jiqie',
            'id1805'=>'jiqie',
            'id1'=>'30',
            'id2'=>$id2,
            'id3'=>'%230000FF',
            'id4'=>'%230000DD',
            'id5'=>'%230000AA',
            'id6'=>'%23000077',
        );

        $url = "http://www.jiqie.com/a/re22.php";
        $headers=array(
            'Content-Type:application/x-www-form-urlencoded',
        );
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch,CURLOPT_URL,$url); //设置请求地址
        curl_setopt($ch,CURLOPT_POST,true); //post请求
        curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($dataArr));// post请求的数据
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//不需要证书验证
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//不直接输出到页面
        $json = curl_exec($ch);
        preg_match("/http[\s\S]*gif/",$json,$matches);
        return $matches[0];
}

function downloadImg($url,$path){//下载图片
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url); //设置请求地址
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//不需要证书验证
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//不直接输出到页面
    $file = curl_exec($ch);

    $filename = pathinfo($url, PATHINFO_BASENAME);
    $resource = fopen($path . $filename, 'a');
    fwrite($resource, $file);
    fclose($resource);
    curl_close($ch);
    return $path.$filename;
}
function mergeImg($file,$name){//图片合成
    // 图片一
    $path_1 = mb_substr(THINK_PATH,0,-9)."Public/images/mingli.jpg";
   // 图片二
    $path_2 = $file;
   // 创建图片对象
    $image_1 = imagecreatefromjpeg($path_1);
    $image_2 = imagecreatefromgif($path_2);
    // 创建真彩画布
    $image_3 = imageCreatetruecolor(500,350);
    // 为真彩画布创建背景
    $color = imagecolorallocate($image_3, 220, 220, 220);
    imagefill($image_3, 0, 0, $color);
    // 合成图片
    imagecopymerge($image_3, $image_1, 80, 180, 0, 0, imagesx($image_1), imagesy($image_1), 100);
    imagecopymerge($image_3, $image_2, 0, 0, 0, 20, imagesx($image_2), imagesy($image_2), 100);
    // 添加文字水印
    $black = imagecolorallocate($image_3, 0, 0, 0);
    $blackx = imagecolorallocate($image_3, 100, 100, 100);
    $font = mb_substr(THINK_PATH,0,-9).'Public/font/simhei.ttf';
    imagettftext($image_3, 20, 0, imagesx($image_3)-260, imagesy($image_3)-70, $black, $font, "姓名：{$name}");
    imagettftext($image_3, 15, 0, imagesx($image_3)-260, imagesy($image_3)-30, $blackx, $font, '你也来生成一个试试');
    // 输出合成图片
    $returnPath=mb_substr(THINK_PATH,0,-9)."Upload/merge/merge".mt_rand(0,50).".png";
    $boole=imagepng($image_3,$returnPath);

    return $returnPath;
}
function curl($url,$json){
    $ch = curl_init();
    curl_setopt ( $ch, CURLOPT_SAFE_UPLOAD, false);//php5.6 FILE提交特殊处理
    curl_setopt($ch,CURLOPT_URL,$url); //设置请求地址
    curl_setopt($ch,CURLOPT_POST,true); //post请求
    curl_setopt($ch,CURLOPT_POSTFIELDS,$json);// post请求的数据
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//不需要证书验证
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//不直接输出到页面
    $json = curl_exec($ch);
    return $json;
}